package com.yndsoft.jc.ui.shoppingcart;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yndsoft.jc.R;
import com.yndsoft.jc.activity.BaseActivity;
import com.yndsoft.jc.model.KeyValue;
import com.yndsoft.jc.model.Member;
import com.yndsoft.jc.model.Order;
import com.yndsoft.jc.model.OrderDet;
import com.yndsoft.jc.model.ReturnMsg;
import com.yndsoft.jc.netutils.AppUtils;
import com.yndsoft.jc.supplier.utils.ToastUtil;
import com.yndsoft.jc.ui.memberpage.address.AddMailAddressActivity;
import com.yndsoft.jc.ui.memberpage.address.MailingAddressActivity;
import com.yndsoft.jc.ui.memberpage.myorder.MemberAddress;
import com.yndsoft.jc.ui.net.util.ProgressSubscriber;
import com.yndsoft.jc.ui.shoppingcart.entity.CartBean;
import com.yndsoft.jc.ui.shoppingcart.entity.ShoppingItemBean;
import com.yndsoft.jc.utils.ConstantValues;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by hasee on 2017/12/5.
 */

public class ConfirmOrderActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.base_backBtn)
    RelativeLayout base_backBtn;
    @Bind(R.id.base_title)
    TextView base_title;
    @Bind(R.id.shopping_details_order)
    ListView shopping_details_order;
    List<OrderDet> datas;
    ConfirmOrderAdapter adapter;
    View footerView;

    @Bind(R.id.rl_contact_choose)
    RelativeLayout rl_contact_choose;
    @Bind(R.id.rl_no_defaultAds)
    RelativeLayout rl_no_defaultAds;
    @Bind(R.id.send_people)
    TextView send_people;
    @Bind(R.id.send_people_cell)
    TextView send_people_cell;
    @Bind(R.id.send_people_adrs)
    TextView send_people_adrs;

    @Bind(R.id.order_confirm_price)
    TextView order_confirm_price;//总价钱
    @Bind(R.id.order_confirm_weight)
    TextView order_confirm_weight;//总重量

    @Bind(R.id.pay_for_order_btn)
    TextView pay_for_order_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_order);

        setViews();
        setListner();
    }

    private String key = "MyOrder";
    Order order;
    int ctype;
    private double totalPrice;
    private int totalWeight;

    private void setViews() {
        Intent intent = getIntent();
        order = (Order) intent.getSerializableExtra(key);
        ctype = intent.getIntExtra("ctype", 1);
        datas = order.getDets();
        base_title.setText(getResources().getString(R.string.confirm_order));
        initAddress();//寄件地址
        statistics();

        footerView = LayoutInflater.from(mContext).inflate(R.layout.foot_confir_order_list, null);
        shopping_details_order.addFooterView(footerView);

        adapter = new ConfirmOrderAdapter(mContext);
//        adapter.setCheckInterface(this);
        shopping_details_order.setAdapter(adapter);
        adapter.setData(datas);
    }


    MemberAddress member;

    private void initAddress() {
        mHttpUtil.getDefaultAds(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        rl_no_defaultAds.setVisibility(View.VISIBLE);
                        rl_contact_choose.setVisibility(View.GONE);
                        break;
                    case 1:
                        rl_contact_choose.setVisibility(View.VISIBLE);
                        rl_no_defaultAds.setVisibility(View.GONE);
                        Type type = new TypeToken<MemberAddress>() {
                        }.getType();
                        Object fromJson = new Gson().fromJson(returnMsg.getInfo(), type);
                        member = (MemberAddress) fromJson;
                        AppUtils.setText(send_people, member.getName());
                        AppUtils.setText(send_people_cell, member.getPhone());
                        AppUtils.setText(send_people_adrs, member.getAddress());
                        break;
                }
            }
        }, "address", mSession.getUserInfo().getId());
    }

    private void setListner() {
        base_backBtn.setOnClickListener(this);
        rl_contact_choose.setOnClickListener(this);
        rl_no_defaultAds.setOnClickListener(this);
        pay_for_order_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.base_backBtn:
                finish();
                break;
            case R.id.rl_contact_choose://选择地址
                intent = new Intent(this, MailingAddressActivity.class);
                startActivityForResult(intent, ConstantValues.CHANGE_RECEIVE_ADDRESS);
                break;
            case R.id.pay_for_order_btn://提交订单
                submitOrder();
                break;
            case R.id.rl_no_defaultAds://没有默认地址
                intent = new Intent(this, AddMailAddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ConstantValues.CHANGE_RECEIVE_ADDRESS) {
                initAddress();
            }
        }
    }

    private void submitOrder() {
        order.setMemberaddress(member);
        mHttpUtil.confirmOrder(new ProgressSubscriber<ReturnMsg>(mContext) {
            @Override
            public void next(ReturnMsg returnMsg) {
                switch (returnMsg.getCode()) {
                    case 0:
                        ToastUtil.show(mContext, "提交不成功");
                        break;
                    case 1:
                        Intent intent = new Intent(ConfirmOrderActivity.this, PayForOrderActivity.class);
                        intent.putExtra(ConstantValues.CONFIRM_ORDER, totalPrice);
                        startActivity(intent);
                        break;
                }
            }
        }, "toorder", mSession.getUserInfo().getId(), ctype, new Gson().toJson(order));
    }

    public void statistics() {
        totalPrice = 0.00;
        totalWeight = 0;
        for (int i = 0; i < datas.size(); i++) {
            OrderDet shoppingCartBean = datas.get(i);
            if (shoppingCartBean.isChoose()) {
                totalPrice += shoppingCartBean.getProductattribute().getPrice();
                totalWeight += shoppingCartBean.getProductattribute().getLimit();
            }
        }
        AppUtils.setText(order_confirm_price, "￥" + totalPrice);
        AppUtils.setText(order_confirm_weight, "总重量：" + totalWeight + "g(一勺约8g)");
    }
}
